package com.andnnl.xml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlWithCData {
    public static void main(String[] args) {
        ReturnForm form = new ReturnForm();
        List<PushSmsSelForm> list=new ArrayList<PushSmsSelForm>();
        final PushSmsSelForm f = new PushSmsSelForm();
        f.setContent("as顶替阿斯蒂芬工<![CDATA[as顶替阿斯蒂芬工顶替]]>顶替");
        list.add(f);
        form.setList(list);
        System.out.println(getFormData(form));
    }
    public static String getFormData(ReturnForm form) {
        try {
            DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
            Document document = documentBuilder.newDocument();

            Element returnFormElement = document.createElement("returnForm");
            document.appendChild(returnFormElement);

            Element typeElement = document.createElement("type");
            typeElement.setTextContent(String.valueOf(form.getType()));
            returnFormElement.appendChild(typeElement);

            Element countElement = document.createElement("count");
            countElement.setTextContent(String.valueOf(form.getCount()));
            returnFormElement.appendChild(countElement);

            Element listElement = document.createElement("list");
            returnFormElement.appendChild(listElement);

            List<PushSmsSelForm> msgList = form.getList();
            for (PushSmsSelForm pushSmsForm : msgList) {
                Element pushSmsFormElement = document.createElement("pushSmsForm");
                listElement.appendChild(pushSmsFormElement);

                Element eprIdElement = document.createElement("eprId");
                eprIdElement.setTextContent(""+pushSmsForm.getEprId());
                pushSmsFormElement.appendChild(eprIdElement);

                Element mobileElement = document.createElement("mobile");
                mobileElement.setTextContent(pushSmsForm.getMobile());
                pushSmsFormElement.appendChild(mobileElement);

                Element msgIdElement = document.createElement("msgId");
                msgIdElement.setTextContent(pushSmsForm.getMsgId());
                pushSmsFormElement.appendChild(msgIdElement);

                Element contentElement = document.createElement("content");
                CDATASection cdata = document.createCDATASection(pushSmsForm.getContent());
                contentElement.appendChild(cdata);
                pushSmsFormElement.appendChild(contentElement);

                Element userIdElement = document.createElement("userId");
                userIdElement.setTextContent(pushSmsForm.getUserId());
                pushSmsFormElement.appendChild(userIdElement);

                //20160929,新增拓展号，显示号码字段
                Element extNumElement = document.createElement("extNum");
                extNumElement.setTextContent(pushSmsForm.getExtnum());
                pushSmsFormElement.appendChild(extNumElement);

                Element recvNumElement = document.createElement("recvNum");
                recvNumElement.setTextContent(pushSmsForm.getRecvnum());
                pushSmsFormElement.appendChild(recvNumElement);
            }

            // 将XML文档转换为字符串
            StringWriter stringWriter = new StringWriter();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
//            transformer.setOutputProperty(OutputKeys.INDENT, "no");//格式化
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // 不打印<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(stringWriter);
            transformer.transform(domSource, streamResult);

            return stringWriter.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}